package hr.fer.pus.dll_will.cr.actions;

import hr.fer.pus.dll_will.cr.IActionHandler;
import hr.fer.pus.dll_will.cr.data.FileDescriptor;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

public class RegisterFiles implements IActionHandler {
	
	private List<FileDescriptor> files;
	
	public RegisterFiles(List<FileDescriptor> l){
		this.files = l;
	}
	
	@Override
	public boolean performAction(ServletOutputStream out,
			HttpServletRequest request) throws IOException {
		
		String req = request.getParameter("filelist");
		String[] fileList = req.split("#");
		for (String file : fileList) {
			String[] params = file.split(" ");
			try{
				Long fileID = Long.valueOf(params[0]);
				String fileName = params[1];
				String author = params[2];
				String desc = params[3];
				Long spID = Long.valueOf(params[4]);
				FileDescriptor fds = new FileDescriptor(fileID, fileName, author, desc, spID);
				files.add(fds);
				System.out.println("CR - registrirana datoteka: " + fds.toString() );
			}catch(NumberFormatException e){
				out.write(("Error: "+e.getMessage()).getBytes());
			}
		}
		
		return true;
	}

}
